package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.WxCommentPostBo;
import com.cskaoyan.bean.entity.Comment;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: project2_market
 * @time: 2021/10/22
 * @author: yangshuai
 **/

@RestController
@RequestMapping("wx/comment")
public class WxCommentController {

    static int count = 0;

    @Autowired
    CommentService commentService;

    /**
     * 查询该商品评价数量，有图评价数量
     * @param valueId
     * @return
     */
    @RequestMapping("count")
    public BaseRespVo count(Integer valueId,Integer type){

        WxCommentCountVo commentCountVo = commentService.count(valueId,type);

        return BaseRespVo.ok(commentCountVo);
    }

    /**
     * 查询该商品全部评论，及评价者信息
     * @param valueId
     * @param type
     * @param limit
     * @param page
     * @param showType
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(Integer valueId,Integer type,Integer limit,Integer page,Integer showType){

        if (count != page || page == 1){
            count = page;
        }else {
            return BaseRespVo.fail("已经到底了");
        }
        List<WxCommentListVo> list = commentService.list(valueId, type, limit, page, showType);
//        BasePageData basePageData = commentService.list(valueId,type,limit,page,showType);
        return BaseRespVo.ok(BasePageData.ok(list));
    }

    /**
     * 发表评论
     * @param wxCommentPostBo
     * @param username
     * @return
     */
    @RequestMapping("post")
    public BaseRespVo post(@RequestBody WxCommentPostBo wxCommentPostBo, @SessionAttribute("username") String username){

        // 插入评论
        WxCommentPostVo wxCommentPostVo = commentService.post(wxCommentPostBo,username);
//        Comment comment = commentService.post(wxCommentPostBo,username);

        return BaseRespVo.ok(wxCommentPostVo);
//        return BaseRespVo.ok(comment);
    }

}
